package com.jy.adscm.order.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jy.adscm.dealer.bean.Dealer;
import com.jy.adscm.dealer.mapper.DealerMapper;
import com.jy.adscm.order.bean.JyOrder;
import com.jy.adscm.order.bean.JyOrderChart;
import com.jy.adscm.order.mapper.JyOrderMapper;
import com.jy.adscm.order.service.JyOrderChartService;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zyl
 * @Date 2019/1/27 -10:02
 */
@Service
public class JyOrderChartServiceImpl implements JyOrderChartService {

    @Autowired
    JyOrderMapper jyOrderMapper;
    @Autowired
    DealerMapper dealerMapper;
    @Override
    public List<JyOrderChart> selectOrderChart( String startTime, String endTime, Token token) {
        List<JyOrderChart>  jyOrderCharts =new ArrayList<>();
        Wrapper<JyOrder>  wrapper=new EntityWrapper<>();
        wrapper.eq("order_state" ,0);
        wrapper.ge("predict_time",startTime);
        wrapper.le("predict_time",endTime);
        List<JyOrder> jyOrders = jyOrderMapper.selectList(wrapper);
        for (JyOrder jyOrder : jyOrders) {
            JyOrderChart jyOrderChart=new JyOrderChart();
            jyOrderChart.setId(jyOrder.getId());
            if(AdscmBaseUtils.getInstance().isNotEmpty(jyOrder.getDealer(),null)){
                Dealer dealer = dealerMapper.selectById(jyOrder.getDealer());
                jyOrderChart.setDealer(dealer.getName());
            }
                  jyOrderChart.setState("0");
                  jyOrderChart.setOrderTime(jyOrder.getCreateTime());
                  jyOrderChart.setPredictTime(jyOrder.getPredictTime());
                 jyOrderCharts.add(jyOrderChart);
        }
        Wrapper<JyOrder>  wrapper1=new EntityWrapper<>();
        wrapper1.eq("order_state" ,2);
        wrapper1.ge("sure_predict_time",startTime);
        wrapper1.le("sure_predict_time",endTime);
        List<JyOrder> jyOrders1 = jyOrderMapper.selectList(wrapper1);
        for (JyOrder jyOrder : jyOrders1) {
            JyOrderChart jyOrderChart=new JyOrderChart();
            jyOrderChart.setId(jyOrder.getId());
            if(AdscmBaseUtils.getInstance().isNotEmpty(jyOrder.getDealer(),null)){
                Dealer dealer = dealerMapper.selectById(jyOrder.getDealer());
                jyOrderChart.setDealer(dealer.getName());
            }
                jyOrderChart.setState("1");
                jyOrderChart.setOrderTime(jyOrder.getCreateTime());
                jyOrderChart.setPredictTime(jyOrder.getSurePredictTime());
                jyOrderCharts.add(jyOrderChart);
        }
        return jyOrderCharts;
    }
}
